//
//  SENewfeatureCVC.m
//  RealDoc
//
//  Created by 蒋小哥 on 16/7/11.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SENewfeatureCVC.h"
#import "SENewsFeatureCell.h"
#define kCVSize (self.collectionView.bounds.size)

@interface SENewfeatureCVC ()

/**
 *  存放所有背景图片的数组
 */
@property (nonatomic, strong) NSArray<UIImage *> *imagesArr;

//存放title
@property (nonatomic, strong)NSArray<NSString *> * titleAray;
//存放标语
@property (nonatomic, strong)NSArray * detailArray;

/**
 *  程序截图
 */
@property (nonatomic, strong) UIImageView *PhotoView;
/**
 *  大文字
 */
@property (nonatomic, strong) UILabel *lb_title;

@property (nonatomic, strong)UILabel * lb_detail;

/**
 *  x的偏移量
 */
@property (nonatomic, assign)CGFloat titleX;
/**
 *  x的偏移量
 */
@property (nonatomic, assign)CGFloat detailX;

@property (nonatomic, strong)UIPageControl * pageControl;

@end

@implementation SENewfeatureCVC

static NSString * const reuseIdentifier = @"Cell";

#pragma mark 界面布局
- (instancetype)init
{
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    
    return [super initWithCollectionViewLayout:flowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.bounces = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [self.collectionView registerClass:[SENewsFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self setupUI];
}

- (void)setupUI {
    //主题lb
    UILabel * lb_title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 172, 50)];
    lb_title.text = self.titleAray[0];
    lb_title.font = FONT(43);
   lb_title.x = [UIScreen mainScreen].bounds.size.width - kCVSize.width * 0.1 - lb_title.bounds.size.width;
    self.titleX = lb_title.x;
    lb_title.y = kCVSize.height* 0.75;
    [lb_title setTextColor:[UIColor magentaColor]];
    [self.collectionView addSubview:lb_title];
    
    //标语lb
    UILabel * lb_detail = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 264, 50)];
    lb_detail.text = self.detailArray[0];
    lb_detail.font = FONT(22);
    lb_detail.x = [UIScreen mainScreen].bounds.size.width - kCVSize.width * 0.1 - lb_detail.bounds.size.width;
    self.detailX = lb_detail.x;
    lb_detail.y = kCVSize.height * 0.8;
    [lb_detail setTextColor:[UIColor magentaColor]];
    [lb_detail setTextAlignment:NSTextAlignmentRight];
    [self.collectionView addSubview:lb_detail];
    
    //程序截图
    UIImageView * imgv_icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kCVSize.width * 0.7, kCVSize.height * 0.55)];
    imgv_icon.x = kCVSize.width * 0.1;
    imgv_icon.y = kCVSize.height * 0.05;
    imgv_icon.image = [UIImage imageNamed:@"jt1"];
    [self.collectionView addSubview:imgv_icon];
    
    self.PhotoView = imgv_icon;
    self.lb_detail = lb_detail;
    self.lb_title = lb_title;
    self.PhotoView.alpha = 0.95;
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    _pageControl.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, kCVSize.height * 0.97);
    _pageControl.numberOfPages = self.imagesArr.count;
    _pageControl.pageIndicatorTintColor = [UIColor redColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
    [self.view addSubview:self.pageControl];
}

#pragma mark 代理方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 1.获取在屏幕x轴方向的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 1.2 判断方向
    if (self.PhotoView.x > offsetX) {

        self.PhotoView.x = offsetX - scrollView.bounds.size.width;
        
    } else {
        
        self.PhotoView.x = offsetX + scrollView.bounds.size.width;
    }
    
    // 2.切换图片
    // 2.0 计算索引
    NSInteger idx = offsetX / scrollView.bounds.size.width;
    
    NSString *iconName = [NSString stringWithFormat:@"jt%@", @(idx + 1)];

    self.PhotoView.image = [UIImage imageNamed:iconName];
    self.lb_title.text = self.titleAray[idx];
    self.lb_detail.text = self.detailArray[idx];

    // 3.修改frame,保证能够看到!
    [UIView animateWithDuration:0.5 animations:^{
        
        self.PhotoView.x = offsetX + kCVSize.width * 0.1;
        self.lb_title.x = offsetX + self.titleX;
        self.lb_detail.x = offsetX + self.detailX;
    }];
    self.pageControl.currentPage = idx;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.imagesArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SENewsFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.backgroundView = [[UIImageView alloc]initWithImage:self.imagesArr[indexPath.item]];
    
    if (indexPath.item == self.imagesArr.count - 1) {
        cell.goBtn.hidden = NO;
    }else {
        cell.goBtn.hidden = YES;
    }

    return cell;
}

#pragma mark 懒加载
- (NSArray<UIImage *> *)imagesArr {
    if (_imagesArr == nil) {
        _imagesArr = @[
                       [UIImage imageNamed:@"01"],
                       [UIImage imageNamed:@"02"],
                       [UIImage imageNamed:@"03"],
                       [UIImage imageNamed:@"04"]
                       ];
    }
    return _imagesArr;
}

- (NSArray<NSString *> *)titleAray {
    if (nil == _titleAray) {
        _titleAray = @[@"病例管理",@"名医申请",@"关注医生",@"更多功能"];
    }
    return _titleAray;
}

- (NSArray *)detailArray {
    if (nil == _detailArray) {
        _detailArray = @[@"亲手管理您的健康",@"名医面对面,想约就能约",@"发现好医生?赶紧来关注吧",@"请点击立即体验"];
    }
    return _detailArray;
}
@end











